#include "stdafx.h"
#include "QMainFrame.h"
#include "QConfGames.h"
#include "QooleId.h"

BEGIN_EVENT_TABLE(QConfGames, wxPanel)
	EVT_CHOICE(QID_CONF_GAME_CHOICE, QConfGames::OnGameChoice)
	EVT_BUTTON(QID_CONF_GAME_BROWSE, QConfGames::OnGameBrowse)
	EVT_CHOICE(QID_CONF_UTIL_CHOICE, QConfGames::OnUtilChoice)
	EVT_BUTTON(QID_CONF_UTIL_BROWSE, QConfGames::OnUtilBrowse)
END_EVENT_TABLE()

QConfGames::QConfGames(wxWindow *parent)
	: wxPanel(parent)
{
	// Create the layout.
	wxBoxSizer *panelSizer = new wxBoxSizer(wxVERTICAL);

	// Game selection.
	wxBoxSizer *gameSizer = new wxBoxSizer(wxHORIZONTAL);
	panelSizer->Add(gameSizer, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

	wxStaticText *label = new wxStaticText(this, wxID_ANY, _("Game"));
	gameChoice = new wxChoice(this, QID_CONF_GAME_CHOICE);
	gameSizer->Add(label, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT, 5);
	gameSizer->Add(gameChoice);

	// Map compile.
	wxStaticBoxSizer *mapBox = new wxStaticBoxSizer(wxVERTICAL, this,
			_("Map Compile Utilities"));
	panelSizer->Add(mapBox, 0, wxEXPAND|wxALL, 5);

	wxFlexGridSizer *mapSizer = new wxFlexGridSizer(3, 2);
	mapSizer->AddGrowableCol(1, 1);
	mapBox->Add(mapSizer, 0, wxEXPAND);

	// Program choice
	label = new wxStaticText(this, wxID_ANY, _("Program"));
	utilChoice = new wxChoice(this, QID_CONF_UTIL_CHOICE);
	mapSizer->Add(label, 0, wxALIGN_CENTER_VERTICAL);
	mapSizer->Add(utilChoice, 0, wxEXPAND);
	mapSizer->AddStretchSpacer(0);

	// Program path.
	label = new wxStaticText(this, wxID_ANY, _("Path"));
	utilPath = new wxTextCtrl(this, wxID_ANY);
	wxButton *utilBrowse = new wxButton(this, QID_CONF_UTIL_BROWSE, _("Browse"));
	mapSizer->Add(label, 0, wxALIGN_CENTER_VERTICAL);
	mapSizer->Add(utilPath, 0, wxEXPAND);
	mapSizer->Add(utilBrowse);

	// Game path
	wxStaticBox *gameBox = new wxStaticBox(this, wxID_ANY, _("Game"));
	wxStaticBoxSizer *gameBoxSizer = new wxStaticBoxSizer(gameBox, wxHORIZONTAL);
	panelSizer->Add(gameBoxSizer, 0, wxEXPAND|wxALL, 5);

	label = new wxStaticText(this, wxID_ANY, _("Path"));
	gamePath = new wxTextCtrl(this, QID_CONF_GAME_BROWSE);
	wxButton *gameBrowse = new wxButton(this, QID_CONF_GAME_BROWSE, _("Browse"));
	gameBoxSizer->Add(label, 0, wxALIGN_CENTER_VERTICAL);
	gameBoxSizer->Add(gamePath, 1, wxEXPAND);
	gameBoxSizer->Add(gameBrowse);

	// Set the sizer.
	SetSizer(panelSizer);
	panelSizer->Fit(this);

	// Fill the game list.
	IterLinkList<Game> iter(Game::GetGames());
	Game *game;
	int i, gameIndex= 0;
	QooleDoc *document = GetMainFrame()->GetDeskTopDocument();

	iter.Reset();
	i = 0;
	while(!iter.IsDone())
	{
		game = iter.GetNext();

		// Add the game.
		gameChoice->Append(game->GetName(), (void*)game);

		// If the current document is using it, select it.
		if (document != NULL && document->GetGame() == game)
			gameIndex = i;

		// Store the index.
		++i;
	}

	// Select the game.
	currentGame = NULL;
	gameChoice->SetSelection(gameIndex);
	SelectGame();
}

QConfGames::~QConfGames()
{
	for(size_t i = 0; i < gameConfs.size(); ++i)
		delete gameConfs[i];
}

void QConfGames::OnGameChoice(wxCommandEvent &ev)
{
	SelectGame();
}

void QConfGames::OnGameBrowse(wxCommandEvent &ev)
{
	wxFileName oldGamePath = gamePath->GetValue();
	wxFileDialog dlg(this, _("Choose the game file."), oldGamePath.GetPath(),
			oldGamePath.GetFullName(), wxT("*"), wxOPEN);
	if(dlg.ShowModal() == wxID_OK)
		gamePath->SetValue(dlg.GetPath());
}

void QConfGames::OnUtilChoice(wxCommandEvent &ev)
{
	// Store the old util path.
	currentGame->utilPaths[currentGame->selectedUtil] = utilPath->GetValue();

	// Read the new util path.
	currentGame->selectedUtil = utilChoice->GetSelection();
	utilPath->SetValue(currentGame->utilPaths[currentGame->selectedUtil]);
}

void QConfGames::OnUtilBrowse(wxCommandEvent &ev)
{
	wxFileName oldUtilPath = utilPath->GetValue();
	wxFileDialog dlg(this, _("Choose the game file."), oldUtilPath.GetPath(),
			oldUtilPath.GetFullName(), wxT("*"), wxOPEN);
	if(dlg.ShowModal() == wxID_OK)
		utilPath->SetValue(dlg.GetPath());
}

void QConfGames::SelectGame()
{
	// Store current game data.
	if(currentGame != NULL)
		StoreCurrentData();

	// Get the selected game.
	int gameIndex = gameChoice->GetSelection();
	Game *selectedGame = (Game*)gameChoice->GetClientData(gameIndex);

	// Find a game copy.
	currentGame = NULL;
	for(size_t i = 0; i < gameConfs.size(); ++i)
	{
		GameConfData *gameConf = gameConfs[i];
		if(gameConf->game == selectedGame)
		{
			currentGame = gameConf;
			break;
		}
	}

	// Copy the game data.
	if(currentGame == NULL)
	{
		// Create the game configuration data.
		currentGame = new GameConfData();
		currentGame->game = selectedGame;
		gameConfs.push_back(currentGame);

		// Store the game path.
		int numUtils = selectedGame->GetNumUtils();
		currentGame->gamePath = selectedGame->GetUtilPath(numUtils - 1);

		// Store the other utilities.
		currentGame->selectedUtil = 0;
		currentGame->utilPaths.reserve(numUtils-1);
		for(int i = 0; i < numUtils-1; ++i)
			currentGame->utilPaths.push_back(selectedGame->GetUtilPath(i));
	}

	// Load the game utils.
	utilChoice->Clear();
	for(size_t i = 0; i < currentGame->utilPaths.size(); ++i)
		utilChoice->Append(currentGame->game->GetUtilName(i));
	utilPath->SetValue(currentGame->utilPaths[currentGame->selectedUtil]);
	utilChoice->SetSelection(currentGame->selectedUtil);

	// Load the game path.
	gamePath->SetValue(currentGame->gamePath);

}

void QConfGames::StoreCurrentData()
{
	// Store the current util path.
	int utilIndex = utilChoice->GetSelection();
	currentGame->utilPaths[utilIndex] = utilPath->GetValue();

	// Store the current game path.
	currentGame->gamePath = gamePath->GetValue();
}

void QConfGames::ApplyChanges()
{
	// Store the current game data.
	StoreCurrentData();

	for(size_t i = 0; i < gameConfs.size(); ++i)
	{
		GameConfData *data = gameConfs[i];
		Game *game = data->game;

		// Store the utilities path.
		for(size_t j = 0; j < data->utilPaths.size(); ++j)
			game->SetUtilPath(j, data->utilPaths[j]);

		// Store the game path.
		game->SetUtilPath(data->utilPaths.size(), data->gamePath);
	}
}



